
/////// input.cpp ////////////////////////////////////////////////////////
//    Author:        Richard (richard.lindholm85@gmail.com)             //
//    Project:    some game project                                     //
//    Purpose:    Input class, for all sort of input to the game        //
//////////////////////////////////////////////////////////////////////////

#include "input.h"

// for some reason Visual Studio 2010 thinks 
// the constructor have a return type ... the ; solves that
;
cInput::cInput(HWND hWnd, HINSTANCE hInstance)
{
    m_mousePosition = MousePosition(0, 0, 0);

    m_lpDirectInput = 0;
    m_lpKeyboard = 0;
    m_lpMouse = 0;

    m_hWnd = hWnd;
    
    memset((void*)m_ucKeyState, 0, 256);
    memset((void*)m_ucPrevKeyState, 0, 256);
    
    memset((void*)&m_mouseState, 0, sizeof(DIMOUSESTATE));
    memset((void*)&m_prevMouseState, 0, sizeof(DIMOUSESTATE));

    DirectInput8Create(
        hInstance,
        DIRECTINPUT_VERSION,
        IID_IDirectInput8,
        (void**)&m_lpDirectInput,
        0);

    m_lpDirectInput->CreateDevice( GUID_SysKeyboard, &m_lpKeyboard, NULL );

    m_lpKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
    m_lpKeyboard->SetDataFormat(&c_dfDIKeyboard);
    m_lpKeyboard->Acquire();

    m_lpDirectInput->CreateDevice(GUID_SysMouse, &m_lpMouse, 0);

    m_lpMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
    m_lpMouse->SetDataFormat(&c_dfDIMouse);
    m_lpMouse->Acquire();
}

void cInput::updateInput()
{
    updateKeyboard();
    updateMouse();
}

void cInput::updateKeyboard()
{
    memcpy((void*)m_ucPrevKeyState, (void*)m_ucKeyState, 256);
    memset((void*)m_ucKeyState, 0, 256);

    HRESULT result = m_lpKeyboard->GetDeviceState(256, (LPVOID)m_ucKeyState);
    
    if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
        m_lpKeyboard->Acquire();
}

void cInput::updateMouse()
{
    memcpy((void*)&m_prevMouseState, (void*)&m_mouseState, sizeof(DIMOUSESTATE));
    memset((void*)&m_mouseState, 0, sizeof(DIMOUSESTATE));

    HRESULT result = m_lpMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
    
    if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
        m_lpMouse->Acquire();
    
    // this will help get the mouse position from windows,
    // the DirectInput one is better in fullscreen
    POINT mousePos;
    GetCursorPos(&mousePos);
    RECT windowRect;
    GetWindowRect(m_hWnd, &windowRect);

    m_mousePosition.xPos = (int)(mousePos.x - windowRect.left);
    m_mousePosition.yPos = (int)(mousePos.y - windowRect.top);
    m_mousePosition.zPos += 0;
}

// Keyboard functions
bool cInput::isKeyDown(DWORD keyID)
{
    //// check if keyID is down
    //DWORD result = XInputGetKeystroke(0, 0, XINPUT_KEYSTROKE(keyID, 0, XINPUT_KEYSTROKE_KEYDOWN, 0, 0));

    //// if the key is down, return true
    //if (result == ERROR_SUCCESS) return true;

    //// if not, return false
    //return false;
    
    if (m_ucKeyState[keyID] & 0x80)
        return true;
    return false;
}

bool cInput::isKeyUp(DWORD keyID)
{
    //// check if keyID is up
    //DWORD result = XInputGetKeystroke(0, 0, XINPUT_KEYSTROKE(keyID, 0, XINPUT_KEYSTROKE_KEYUP, 0, 0));

    //// if the key is up, return true
    //if (result == ERROR_SUCCESS) return true;

    //// if not, return false
    //return false;
    return false;
}

bool cInput::hasKeyBeenPushed(DWORD keyID)
{
    if (m_ucKeyState[keyID] & 0x80)
    {
        if (!(m_ucPrevKeyState[keyID] & 0x80))
        {
            return true;
        }
    }

    return false;
}


// Mouse functions
bool cInput::isMouseKeyDown(int buttonID)
{
    if (m_mouseState.rgbButtons[buttonID] & 0x80)
        return true;

    return false;
}

bool cInput::isMouseKeyUp(int buttonID)
{
    if (!(m_mouseState.rgbButtons[buttonID] & 0x80))
        return true;

    return false;
}

bool cInput::hasMouseKeyBeenPushed(int buttonID)
{
    if (isMouseKeyDown(buttonID))
        if (isMouseKeyUp(buttonID))
            return true;

    return false;
}


int cInput::getMouseX()
{
    return m_mousePosition.xPos;
}

int cInput::getMouseY()
{
    return m_mousePosition.yPos;
}

int cInput::getMouseZ()
{
    return m_mousePosition.zPos;
}
